package epimarket.helper;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Utils {
	public static boolean isAlphanumeric(String str) {
	    for (int i=0; i<str.length(); i++) {
	        char c = str.charAt(i);
	        if (c < 0x30 || (c >= 0x3a && c <= 0x40) || (c > 0x5a && c <= 0x60) || c > 0x7a)
	            return false;
	    }
	    return true;
	}
	
	public static String sha1(String input) {
		String retour = input;
		try {
			MessageDigest mDigest = MessageDigest.getInstance("SHA1");
			byte[] result = mDigest.digest(input.getBytes());
			StringBuffer sb = new StringBuffer();
			for (int i = 0; i < result.length; i++) {
				sb.append(Integer.toString((result[i] & 0xff) + 0x100, 16).substring(1));
			}
			retour = sb.toString();
		} catch (Exception e) {
		}
        return retour;
    }

	public static boolean isDate(String date) {
		Pattern p = Pattern.compile("[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}");
		Matcher m = p.matcher(date);
		return m.matches();
	}
}
